/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-15
 * Time: 17:52
 */
public class Solution {
    public static void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        int n = arr.length;
        //遍历数组找到最后一个要复写的数
        while (cur < n) {
            if (arr[cur] == 0) {
                dest += 2;
            }else {
                dest++;
            }

            if(dest >= n - 1) {
                break;
            }
            cur++;
        }
        //处理边界情况
        if (dest == n) {
            arr[n - 1] = 0;
            cur--;
            dest -= 2;
        }

        //从后往前复写
        while (cur >= 0) {
            if (arr[cur] == 0) {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }else {
                arr[dest] = arr[cur];
                dest--;
                cur--;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 0 , 2 ,3, 0 , 4, 5 , 0};
        duplicateZeros(arr);
    }
}